package com.scalar.chiptrack.tags;

import com.scalar.chiptrack.utils.ConfigManager;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;

/**
 * User: Sujan
 * Date: Mar 21, 2007
 * Time: 11:11:43 AM
 */
public class InternalCommentsTag extends TagSupport
{
    private String commentsGroupId;
    public final static String PARAM_COMMENTS_GROUP_ID_AUTO = "commentsgroupidauto";
    public String getCommentsGroupId()
    {
        return commentsGroupId;
    }

    public void setCommentsGroupId(String commentsGroupId)
    {
        this.commentsGroupId = commentsGroupId;
    }


    public int doEndTag() throws JspException
    {
        JspWriter out = pageContext.getOut();
        try
        {
            if("auto".equalsIgnoreCase(commentsGroupId)){
                commentsGroupId = pageContext.getRequest().getParameter(PARAM_COMMENTS_GROUP_ID_AUTO);
                if(commentsGroupId == null || commentsGroupId.trim().length()==0) {
                    commentsGroupId = "auto_"+System.currentTimeMillis();
                }
                out.println("<input type='hidden' name='"+PARAM_COMMENTS_GROUP_ID_AUTO+"' value='"+commentsGroupId+"'>\n");
            }
            out.println ( "<script src='/" + ConfigManager.getChipTracContext() + "/jsp/includes/scripts/ajax_utils.js'></script>" );
            out.println ( "<script src='/" + ConfigManager.getChipTracContext() + "/jsp/includes/scripts/ajax_internal_comments.js'></script>" );
            out.println ( "<script>chipTracContext='/" + ConfigManager.getChipTracContext() + "';</script>" );
            out.println ( "<table width='100%'>\n" +
                    "           <tr width='100%'><td align='center' class='NArialS'><font color='blue'>[Click on bar to Expand or Collapse Internal Comments]</font></td></tr>\n" +
                    "           <tr width='100%'>\n" +
                    "               <td CLASS='simpleBorder' style='cursor:hand;' width='100%' onclick=\"toggleInternalComments('" + getCommentsGroupId() + "')\" onMouseOver=\"window.status='Click here to Expand/Collapse';return true\" onmouseout=\"window.status='';return true\"><b>Internal Comments</b></td>\n" +
                    "           </tr>\n" +
                    "           <tr>\n" +
                    "               <td>\n" +
                    "                   <table border=0 id='internalCommentsTable'>\n" +
                    "                       <thead id='internalCommentsHead'></thead>\n" +
                    "                       <tfoot id='internalCommentsFoot'></tfoot>\n" +
                    "                   </table>\n" +
                    "               </td>\n" +
                    "           </tr>\n" +
                    "      </table>" );
            out.println ( "<script > toggleInternalComments('" + getCommentsGroupId() + "')</script>" );
            
        }
        catch ( IOException e )
        {

        }
        return( EVAL_PAGE );
    }
}
